package cloud.maque.module.system.service.impl;

import cloud.maque.common.core.exceptions.BusinessException;
import cloud.maque.common.core.request.PageRequest;
import cloud.maque.common.core.utils.TokenUserUtil;
import cloud.maque.common.core.utils.UserPasswordUtil;
import cloud.maque.common.utils.PageRequestUtil;
import cloud.maque.module.system.entity.SysPrivilege;
import cloud.maque.module.system.entity.SysUser;
import cloud.maque.module.system.mapper.UserMapper;
import cloud.maque.module.system.model.UserEditPassworkDto;
import cloud.maque.module.system.service.UserService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser> implements UserService {

    @Autowired
    PageRequestUtil util;

    @Autowired
    TokenUserUtil userUtil;

    @Override
    public IPage<SysUser> GetList(PageRequest<SysUser> pageReq) {
        Page<SysUser> page = util.GetPage(SysPrivilege.class, pageReq);
        QueryWrapper<SysUser> query = new QueryWrapper<>();
        if (!StrUtil.isEmptyIfStr(pageReq.getData().getUserName())) {
            query.lambda().like(SysUser::getUserName, pageReq.getData().getUserName());
        }
        query.lambda().orderByDesc(SysUser::getCreateTime);
        return this.page(page, query);
    }

    @Override
    public SysUser NewUser(SysUser data) {
        Assert.hasText(data.getUserName(), "用户名不能为空");

        data.setCreateBy(userUtil.GetUserName());
        data.setCreateTime(new Date());
        data.setUpdateTime(new Date());

        SysUser oldPost = GetByUserName(data.getUserName());
        if (oldPost != null && !oldPost.getUserId().equals(data.getUserId())) {
            throw new BusinessException("用户名已存在");
        }

        this.baseMapper.insert(data);

        return data;

    }

    @Override
    public SysUser EditUser(SysUser data) {
        return null;
    }

    private SysUser GetByUserName(String userName) {

        QueryWrapper<SysUser> query = new QueryWrapper<>();
        query.lambda().eq(SysUser::getUserName, userName);
        return this.getOne(query, false);
    }


    @Override
    public SysUser EditStatus(SysUser data) {
        Assert.hasText(data.getUserId(), "ID不能为空");

        SysUser post = this.getById(data.getUserId());
        if (post == null) {
            throw new BusinessException("用户不存在");
        }
        if (post.getDisabled() != null && post.getDisabled().equals(1)) {
            post.setDisabled(0);
        } else {
            post.setDisabled(1);
        }
        UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysUser::getUserId, post.getUserId());
        updateWrapper.lambda().set(SysUser::getDisabled, post.getDisabled());
        updateWrapper.lambda().set(SysUser::getUpdateTime, new Date());
        this.update(updateWrapper);
        return post;
    }

    /**
     * 逻辑删除
     *
     * @param userId
     * @return
     */
    @Override
    public Boolean DeleteUser(String userId) {
        UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysUser::getUserId, userId).set(SysUser::getDelFlag, "1");
        return this.update(updateWrapper);
    }

    @Override
    public Boolean EditPassword(UserEditPassworkDto data) {
        Assert.hasText(data.getUserId(), "ID不能为空");

        SysUser post = this.getById(data.getUserId());
        if (post == null) {
            throw new BusinessException("用户不存在");
        }

        if (!StrUtil.isBlank(post.getPassWord())) {
            if (!UserPasswordUtil.verify(data.getOldPassword(), post.getPassWord(), post.getUserName())) {
                throw new BusinessException("旧密码不正确");
            }
        }

        String newPwd = UserPasswordUtil.encrypt(data.getNewPassword(), post.getUserName());
        UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysUser::getUserId, post.getUserId());
        updateWrapper.lambda().set(SysUser::getPassWord, newPwd);
        updateWrapper.lambda().set(SysUser::getUpdateTime, new Date());
        return this.update(updateWrapper);
    }
}
